home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Personal Computer World 2009 February
/
PCWFEB09.iso
/
Software
/
Resources
/
Chat & Communication
/
Digsby build 37
/
digsby_setup.exe
/
lib
/
ZSI
/
ServiceProxy.pyo
(
.txt
)
< prev
next >
Wrap
Python Compiled Bytecode
|
2008-10-13
|
11KB
|
337 lines
# Source Generated with Decompyle++
# File: in.pyo (Python 2.5)
import weakref
import re
import os
import sys
from ConfigParser import SafeConfigParser as ConfigParser, NoSectionError, NoOptionError
from urlparse import urlparse
from ZSI import TC
from ZSI.client import _Binding
from ZSI.generate import commands, containers
from ZSI.schema import GED, GTD
import wstools
def _urn_to_module(urn):
return '%s_types' % re.sub(_urn_to_module.regex, '_', urn)
_urn_to_module.regex = re.compile('[\\W]')
class ServiceProxy:
def __init__(self, wsdl, url = None, service = None, port = None, cachedir = os.path.join(os.path.expanduser('~'), '.zsi_service_proxy_dir'), asdict = True, lazy = False, pyclass = False, force = False, **kw):
self._asdict = asdict
self._url = url
self._kw = kw
self._wsdl = wstools.WSDLTools.WSDLReader().loadFromURL(wsdl)
if not service:
pass
self._service = self._wsdl.services[0]
self.__doc__ = self._service.documentation
if not port:
pass
self._port = self._service.ports[0]
self._name = self._service.name
self._methods = { }
self._cachedir = cachedir
self._lazy = lazy
self._pyclass = pyclass
self._force = force
port = self._port
binding = port.getBinding()
portType = binding.getPortType()
for port in self._service.ports:
for item in port.getPortType().operations:
try:
callinfo = wstools.WSDLTools.callInfoFromWSDL(port, item.name)
except:
continue
method = MethodProxy(self, callinfo)
setattr(self, item.name, method)
self._methods.setdefault(item.name, []).append(method)
self._mod = self._load(wsdl)
def _load(self, location):
cachedir = self._cachedir
if not os.path.isdir(cachedir):
os.mkdir(cachedir)
file = os.path.join(cachedir, '.cache')
section = 'TYPES'
cp = ConfigParser()
try:
cp.readfp(open(file, 'r'))
except IOError:
del cp
cp = None
option = location.replace(':', '-')
if not (self._force) and cp is not None and cp.has_section(section) and cp.has_option(section, option):
types = cp.get(section, option)
elif not self._pyclass:
containers.ContainerBase.func_aname = lambda instnc, n: str(n)
args = [
'-o',
cachedir,
location]
if self._lazy:
args.insert(0, '-l')
if self._pyclass:
args.insert(0, '-b')
files = commands.wsdl2py(args)
if cp is None:
cp = ConfigParser()
if not cp.has_section(section):
cp.add_section(section)
types = filter((lambda f: f.endswith('_types.py')), files)[0]
cp.set(section, option, types)
cp.write(open(file, 'w'))
if os.path.abspath(cachedir) not in sys.path:
sys.path.append(os.path.abspath(cachedir))
mod = os.path.split(types)[-1].rstrip('.py')
return __import__(mod)
def _load_schema(self, location, xml = None):
cachedir = self._cachedir
if not os.path.isdir(cachedir):
os.mkdir(cachedir)
file = os.path.join(cachedir, '.cache')
section = 'TYPES'
cp = ConfigParser()
try:
cp.readfp(open(file, 'r'))
except IOError:
del cp
cp = None
option = location.replace(':', '-')
if cp is not None and cp.has_section(section) and cp.has_option(section, option):
types = cp.get(section, option)
elif not self._pyclass:
containers.ContainerBase.func_aname = lambda instnc, n: str(n)
XMLSchema = XMLSchema
import ZSI.wstools
reader = XMLSchema.SchemaReader(base_url = location)
if xml is not None and isinstance(xml, basestring):
schema = reader.loadFromString(xml)
elif xml is not None:
raise RuntimeError, 'Unsupported: XML must be string'
elif not os.path.isfile(location):
schema = reader.loadFromURL(location)
else:
schema = reader.reader.loadFromFile(location)
class options('options', ()):
output_dir = cachedir
schema = True
simple_naming = False
address = False
lazy = self._lazy
complexType = self._pyclass
schema.location = location
files = commands._wsdl2py(options, schema)
if cp is None:
cp = ConfigParser()
if not cp.has_section(section):
cp.add_section(section)
types = filter((lambda f: f.endswith('_types.py')), files)[0]
cp.set(section, option, types)
cp.write(open(file, 'w'))
if os.path.abspath(cachedir) not in sys.path:
sys.path.append(os.path.abspath(cachedir))
mod = os.path.split(types)[-1].rstrip('.py')
return __import__(mod)
def _call(self, name, soapheaders):
def call_closure(*args, **kwargs):
if len(args) and len(kwargs):
raise TypeError, 'Use positional or keyword argument only.'
if len(args) > 0:
raise TypeError, 'Not supporting SOAPENC:Arrays or XSD:List'
if len(kwargs):
args = kwargs
callinfo = getattr(self, name).callinfo
for method in self._methods[name]:
if len(method.callinfo.inparams) == len(kwargs):
callinfo = method.callinfo
continue
if not self._url:
pass
binding = _Binding(url = callinfo.location, soapaction = callinfo.soapAction, **self._kw)
kw = dict(unique = True)
if callinfo.use == 'encoded':
kw['unique'] = False
if callinfo.style == 'rpc':
request = TC.Struct(None, ofwhat = [], pname = (callinfo.namespace, name), **kw)
response = TC.Struct(None, ofwhat = [], pname = (callinfo.namespace, name + 'Response'), **kw)
if len(callinfo.getInParameters()) != len(args):
raise RuntimeError('expecting "%s" parts, got %s' % str(callinfo.getInParameters(), str(args)))
for msg, pms in ((request, callinfo.getInParameters()), (response, callinfo.getOutParameters())):
msg.ofwhat = []
for klass in pms:
part = None
if klass is None:
if part.type:
klass = (filter,)((lambda gt: part.type == gt.type), TC.TYPES)
if len(klass) == 0:
klass = (filter,)((lambda gt: part.type[1] == gt.type[1]), TC.TYPES)
if not len(klass):
klass = [
TC.Any]
if len(klass) > 1:
klass = filter((lambda i: i.__dict__.has_key('type')), klass)
klass = klass[0]
else:
klass = TC.Any
msg.ofwhat.append(klass(part.name))
msg.ofwhat = tuple(msg.ofwhat)
if not args:
args = { }
else:
ipart = callinfo.getInParameters()
opart = callinfo.getOutParameters()
if len(ipart) != 1 and not (ipart[0].element_type) or ipart[0].type is None:
raise RuntimeError, 'Bad Input Message "%s"' % callinfo.name
if len(opart) not in (0, 1) and not (opart[0].element_type) or opart[0].type is None:
raise RuntimeError, 'Bad Output Message "%s"' % callinfo.name
ipart = ipart[0]
request = GED(*ipart.type)
response = None
if opart:
response = GED(*opart[0].type)
msg = args
if self._asdict:
if not msg:
msg = dict()
self._nullpyclass(request)
elif request.pyclass is not None:
if type(args) is dict:
msg = request.pyclass()
msg.__dict__.update(args)
elif type(args) is list and len(args) == 1:
msg = request.pyclass(args[0])
else:
msg = request.pyclass()
binding.Send(None, None, msg, requesttypecode = request, soapheaders = soapheaders, encodingStyle = callinfo.encodingStyle)
if response is None:
return None
if self._asdict:
self._nullpyclass(response)
return binding.Receive(replytype = response, encodingStyle = callinfo.encodingStyle)
return call_closure
def _nullpyclass(cls, typecode):
typecode.pyclass = None
if not hasattr(typecode, 'ofwhat'):
return None
if type(typecode.ofwhat) not in (list, tuple):
cls._nullpyclass(typecode.ofwhat)
else:
for i in typecode.ofwhat:
cls._nullpyclass(i)
_nullpyclass = classmethod(_nullpyclass)
class MethodProxy:
def __init__(self, parent, callinfo):
self.__name__ = callinfo.methodName
self.__doc__ = callinfo.documentation
self.callinfo = callinfo
self.parent = weakref.ref(parent)
self.soapheaders = []
def __call__(self, *args, **kwargs):
return self.parent()._call(self.__name__, self.soapheaders)(*args, **kwargs)
def add_headers(self, **headers):
class _holder:
pass
def _remap(pyobj, **d):
pyobj.__dict__ = d
for k, v in pyobj.__dict__.items():
if type(v) is not dict:
continue
pyobj.__dict__[k] = p = _holder()
_remap(p, **v)
for k, v in headers.items():
h = (filter,)((lambda i: k in i.type), self.callinfo.inheaders)[0]
if h.element_type != 1:
raise RuntimeError, 'not implemented'
typecode = GED(*h.type)
if typecode is None:
raise RuntimeError, 'no matching element for %s' % str(h.type)
pyclass = typecode.pyclass
if pyclass is None:
raise RuntimeError, 'no pyclass for typecode %s' % str(h.type)
if type(v) is not dict:
pyobj = pyclass(v)
else:
pyobj = pyclass()
_remap(pyobj, **v)
self.soapheaders.append(pyobj)